var ws = null // 建立的连接
var lockReconnect = false // 是否真正建立连接
var timeout = 1 * 1000 // 30秒一次心跳
var timeoutObj = null // 心跳倒计时
var serverTimeoutObj = null // 心跳倒计时
var timeoutnum = null // 断开，重连倒计时


function initWebSocket() {
  let id = JSON.parse(localStorage.getItem('user')).id
  // let url = this.wsurl + '/api/webSocket/' + id
  let url = 'ws://192.168.110.112:8002/webSocket/' + id
  ws = new WebSocket(url);
  ws.onopen = function() {
    onopen()
  }
  ws.onmessage = function() {
    onmessage()
  }
  // ws.onclose = function() {
  //   onclose()
  // }
  // ws.onerror = function() {
  //   onerror()
  // }

}


function onopen() {
  console.log('open')
  ws.send("heartCheck"); //这里可以自己跟后端约定
}

function onmessage(e) {
  console.log(e.data)
  if (e.data == 'heartCheck') {
    // to do...
  }
  
}

export default {
  onmessage,
  initWebSocket
}
